import requests
from pyquery import PyQuery as pq
import re
from os.path import exists
from base import BASE_PATH
import os

url = 'https://ssr1.scrape.center/'
html = requests.get(url).text
# 初始化pq对象
doc = pq(html)
items = doc('.el-card').items()

# 目标路径
target_path = BASE_PATH + "/results/data"
if not exists(target_path):
    # 不存在则创建路径
    os.makedirs(target_path)

# 遍历获取每一个项，过滤需要的值，并写入
with open(f'{target_path}/movies.txt', mode='w', encoding='utf-8') as file:
    for item in items:
        name = item.find('h2').text()
        file.write("电影名称:" + name+"\n")
        # print("电影名称:" + name)
        categories = item.find('.categories').text().split(' ')
        # print("剧集类别:" + "".join(categories))
        file.write(f"剧集类别:{categories}"+"\n")
        publish__time = item.find('div.info:last-child span').text()
        # print("发布时间:" + publish__time)
        file.write("发布时间:" + publish__time+"\n")
        score = item.find('.score').text()
        # print("评分:" + score)
        file.write("评分:" + score+"\n")
        # print("*"*50)
        file.write("*" * 50+"\n")
